package com.example.hotelsystem.controller.ocr;



import code.CodeError;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.utils.ocr.AccessTokenUtil;
import com.example.hotelsystem.utils.ocr.Base64Util;
import com.example.hotelsystem.utils.ocr.FileUtil;
import com.example.hotelsystem.utils.ocr.HttpUtil;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-1-1
 */
@RestController
@RequestMapping("/hotelsystem/ocr")
@Api(tags = "OCR身份证识别控制器")
public class OcrController {

    @PostMapping("/aiCheck")
    @ApiOperation("ocr身份证文字识别")
    public Result createOrder(MultipartFile file){
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {
            byte[] imgData =  file.getBytes();
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "id_card_side=" + "front" + "&image=" + imgParam;
            Map<String,Object> map = new HashMap<>();
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AccessTokenUtil.getAuth();
            String result = HttpUtil.post(url, accessToken, param);
            JSONObject json =  JSONObject.fromObject(result);
            JSONObject idcard =  json.getJSONObject("words_result");
            JSONObject jsonName = JSONObject.fromObject(idcard.get("姓名"));
            String name = String.valueOf(jsonName.get("words"));
            JSONObject jsonName1 = JSONObject.fromObject(idcard.get("民族"));
            String nation = String.valueOf(jsonName1.get("words"));
            JSONObject jsonName2 = JSONObject.fromObject(idcard.get("住址"));
            String address = String.valueOf(jsonName2.get("words"));
            JSONObject jsonName3 = JSONObject.fromObject(idcard.get("公民身份号码"));
            String idCard = String.valueOf(jsonName3.get("words"));
            JSONObject jsonName4 = JSONObject.fromObject(idcard.get("出生"));
            String birth = String.valueOf(jsonName4.get("words"));
            JSONObject jsonName5 = JSONObject.fromObject(idcard.get("性别"));
            String sex = String.valueOf(jsonName5.get("words"));


            map.put("name",name);
            map.put("nation",nation);
            map.put("address",address);
            map.put("idcard",idCard);
            map.put("birthday",birth);
            if(sex.equals("男")){
                map.put("sex",1);
            }else {
                map.put("sex",0);
            }


            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeError.INTERNAL_EXCEPTION,e.getMessage());
        }

    }
}
